import { defineStore } from 'pinia'
import { ref } from 'vue'
import {
  get_stockChipList,
  get_stockInfoList,
  get_stockIntradayMovement,
  get_stockIntradayMovementWithDate,
  get_stockPriceList,
} from '@/api/stockApi.ts'
export const useStockStore = defineStore('stockStore', () => {
  const stockSelectList = ref<{
    code: string
    name: string
    exclusive: boolean
  }[]>([])
  // 股票信息
  const stockInfoList = ref<{
    code: string
    name: string
    exclusive: boolean
  }[]>([])

  const stockPriceList = ref<{
    date: string
    code: string
    open: number
    close: number
    high: number
    low: number
    volume: number
    amount: number
    amplitude: number
    price_change_percentage: number
    price_change: number
    turnover_rate: number
    weekday: number
  }[]>([])

  // 筹码数据列表
  const stockChipList = ref<{
    date: string
    code: string
    close: number
    profit_rate: number
    avg: number
    cost_90_L: number
    cost_90_H: number
    concentration_90: number
    cost_70_L: number
    cost_70_H: number
    concentration_70: number
  }[]>([])

  // 日内动量趋势
  const stockIntradayMovement = ref<{
    UpperBound: Record<string, number>
    LowerBound: Record<string, number>
  }>({
    UpperBound: {},
    LowerBound: {},
  })

  // 某日的日内动量
  const stockIntradayMovementWithDate = ref<{
    UpperBound: Record<string, number>
    LowerBound: Record<string, number>
    IntradayPrice: Record<string, number>
  }>({
    UpperBound: {},
    LowerBound: {},
    IntradayPrice: {},
  })

  async function getStockChipList (code: string, window: string) {
    stockChipList.value = []
    const res = await get_stockChipList(code, window)
    if (res.code === 1) {
      stockChipList.value = res.data
    }
    return res.message
  }
  async function getStockInfoList () {
    const res = await get_stockInfoList()
    if (res.code === 1) {
      stockInfoList.value = []
      for (const item of res.data) {
        stockInfoList.value.push(
          {
            code: item.code,
            name: item.name,
            exclusive: false,
          },
        )
      }
    }
  }

  async function getStockPriceList (code: string, window: string, start_date: string, end_date: string) {
    const res = await get_stockPriceList(code, window, start_date, end_date)
    if (res.code === 1) {
      stockPriceList.value = res.data
    }
  }

  async function getStockIntradayMovement (code: string, window: string, period: string) {
    const res = await get_stockIntradayMovement(code, window, period)
    if (res.code === 1) {
      stockIntradayMovement.value = res.data
    }
    return res.message
  }

  async function getStockIntradayMovementWithDate (code: string, window: string, period: string, date: string) {
    const res = await get_stockIntradayMovementWithDate(code, window, period, date)
    if (res.code === 1) {
      stockIntradayMovementWithDate.value = res.data
    }
    return res.message
  }

  return {
    stockSelectList,
    stockInfoList,
    stockPriceList,
    stockChipList,
    stockIntradayMovement,
    stockIntradayMovementWithDate,
    getStockInfoList,
    getStockPriceList,
    getStockChipList,
    getStockIntradayMovement,
    getStockIntradayMovementWithDate,
  }
})
